package com.wuyan.locationdog.ui.device


import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.animation.ObjectAnimator
import android.os.Bundle
import androidx.activity.viewModels
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.DeviceBeanContent
import com.blankj.utilcode.util.ActivityUtils
import com.wuyan.locationdog.databinding.DeviceInspectBinding
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class DeviceInspectActivity : BaseBindingActivity<DeviceInspectBinding>() {
    private val mViewModel: DeviceViewModel by viewModels()

    private var animator: ObjectAnimator? = null

    override fun initData() {
        setTitleText("设备检测")
        val deviceBeanContent = intent.getSerializableExtra("DeviceBeanContent") as DeviceBeanContent


        // 设置动画从0增加到100
        // 设置动画从0增加到100
        animator = ObjectAnimator.ofInt(this, "number", 0, 100)
        animator?.duration = 3000 // 动画持续时间2000毫秒
        animator?.start()

        animator?.addListener(object : AnimatorListener {
            override fun onAnimationStart(p0: Animator) {
            }

            override fun onAnimationEnd(p0: Animator) {
                binding?.apply {
                    if (numberTextView.text.toString().toInt() == 100) {
                        finish()
                        val bundle = Bundle()
                        bundle.putSerializable("DeviceBeanContent", deviceBeanContent)
                        ActivityUtils.startActivity(bundle, DeviceInspectDetailActivity::class.java)
                    }
                }

            }

            override fun onAnimationCancel(p0: Animator) {

            }

            override fun onAnimationRepeat(p0: Animator) {

            }
        })
    }

    // 此方法在动画过程中被调用，更新TextView显示的数字
    fun setNumber(number: Int) {
        binding?.numberTextView?.setText(number.toString())
    }

    override fun onDestroy() {
        super.onDestroy()
        animator?.cancel()
    }
}